package storage

import (
	"gitee.com/igolang/pkg/xcfg"
	"github.com/pkg/errors"
	"mime/multipart"
)

type Handler interface {
	Get(name string) (string, error)
	Save(file *multipart.FileHeader) (*FileInfo, error)
}

var (
	handlerMap = map[string]Handler{
		"local": &localStorage{},
	}
)

func Upload(file *multipart.FileHeader) (*FileInfo, error) {
	handler := GetHandler(xcfg.GetString("storage.select"))
	if handler == nil {
		return nil, errors.New("storage no select")
	}
	return handler.Save(file)
}

func Get(name string) (string, error) {
	handler := GetHandler(xcfg.GetString("storage.select"))
	if handler == nil {
		return "", errors.New("storage no select")
	}
	return handler.Get(name)
}

func GetHandler(key string) Handler {
	return handlerMap[key]
}
